package com.feiyuan.movie.webmagic.eightens;

import com.feiyuan.common.utils.CommonUtils;
import com.feiyuan.movie.vo.Movie80SJiShuVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 80s搜索视频集数
 */
public class Search80SMovieProcessor implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    private String baseUrl;

    public Search80SMovieProcessor(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public Search80SMovieProcessor() {}

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }
    public String getBaseUrl() {
        return baseUrl;
    }

    @Override
    public void process(Page page) {
        // 下载所有视频的文字
        String downlandAllText = page.getHtml().xpath("//form[@id='myform']//ul//li//div//span[@class='dlb_link_link']/text()").get();
        String movieType = CommonUtils.getMovieType(baseUrl);
        if (StringUtils.isNotEmpty(downlandAllText)) {
            // 去掉多余的空格
            downlandAllText = downlandAllText.replaceAll(" ", "");
            if (downlandAllText.equals("载入全部下载资源")) {
                Spider spider = Spider.create(new Download80SAllMovieProcessor(baseUrl));
                ResultItems result = spider.get(baseUrl + "/hd-2");
                if (result != null) {
                    List<Movie80SJiShuVO> movie80SJiShuVOS = result.get("allJiList");
                    page.putField("list", movie80SJiShuVOS);
                }
            }
        } else {
            List<String> allList = page.getHtml().xpath("//ul[@class='dllist1']//li//input/@value").all().stream().filter(item -> {
                if (StringUtils.isNotEmpty(item)) {
                    return true;
                }
                return false;
            }).toList();
            List<String> jiNames = new ArrayList<>();
            if (movieType.equals("movie")) {
                jiNames = page.getHtml().xpath("//ul[@class='dllist1']//li//span[@class='dlname nm']//a/text()").all();
            }
            List<Movie80SJiShuVO> movie80SJiShuVOS = new ArrayList<>();
            int totalNum = allList.size();
            for (int i=0;i<allList.size();i++) {
                Movie80SJiShuVO movie80SJiShuVO = new Movie80SJiShuVO();
                movie80SJiShuVO.setIndex(i);
                if (!CollectionUtils.isEmpty(jiNames)) {
                    movie80SJiShuVO.setPlayName(jiNames.get(i));
                } else {
                    movie80SJiShuVO.setPlayName("第" + totalNum + "集");
                }
                movie80SJiShuVO.setPlayUrl(allList.get(i));
                if (StringUtils.isNotEmpty(allList.get(i))) {
                    movie80SJiShuVOS.add(movie80SJiShuVO);
                }
                totalNum --;
            }
            page.putField("list", movie80SJiShuVOS);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}
